package com.xczl.book.action.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.xczl.book.entity.Book;
import com.xczl.book.entity.BookCate;
import com.xczl.book.service.BookCateService;
import com.xczl.book.service.BookService;

@Controller("bookAdminBookAction")
@RequestMapping("book/admin/book")
public class BookAction {

	@Autowired
	private BookService bookService;

	@Autowired
	private BookCateService bookCateService;

	@RequestMapping("index.htm")
	public String index(Book searchVo,
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap model) {
		PageVo<Book> pageVo = bookService.findPageVo(pageNum, searchVo);
		model.addAttribute("pageVo", pageVo);
		model.addAttribute("searchVo", searchVo);
		return "book/admin/book/index";
	}
	
	@RequestMapping("select.htm")
	public String select(Book searchVo,
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap model) {
		PageVo<Book> pageVo = bookService.findPageVo(pageNum, searchVo);
		model.addAttribute("pageVo", pageVo);
		model.addAttribute("searchVo", searchVo);
		return "book/admin/book/selectBook";
	}

	@RequestMapping("addPage.htm")
	public String addPage(ModelMap modelMap) {
		List<BookCate> list = bookCateService.findAll();
		modelMap.addAttribute("cateList", list);
		return "book/admin/book/add";
	}

	@ResponseBody
	@RequestMapping("add.json")
	public JsonVo<String> add(Book book) {
		bookService.add(book);
		JsonVo<String> json = new JsonVo<String>();// 返回给页面的对象
		json.check();
		return json;
	}

	@RequestMapping("editPage.htm")
	public String editPage(int id, ModelMap model) {
		Book book = bookService.findById(id);
		List<BookCate> catelist = bookCateService.findAll();
		model.addAttribute("book", book);
		model.addAttribute("cateList", catelist);
		return "book/admin/book/edit";
	}

	@ResponseBody
	@RequestMapping("edit.json")
	public JsonVo<String> edit(Book book) {
		bookService.update(book);
		JsonVo<String> json = new JsonVo<String>();// 返回给页面的对象
		json.check();
		return json;
	}
	
	@RequestMapping("editQuantityPage.htm")
	public String editQuantityPage(int id,ModelMap model) {
		Book book = bookService.findById(id);
		model.addAttribute("book", book);
		return "book/admin/book/change";
	}

	@ResponseBody
	@RequestMapping("editQuantity.json")
	public JsonVo<String> editQuantity(int id, int quantity) {
		bookService.editQuantity(id, quantity);
		JsonVo<String> json = new JsonVo<String>();// 返回给页面的对象
		json.check();
		return json;
	}

	@ResponseBody
	@RequestMapping("find.json")
	public JsonVo<Book> findNumber(String number) {
		Book book = bookService.findByNumber(number);
		JsonVo<Book> json = new JsonVo<Book>();// 返回给页面的对象
		if (book == null) {
			json.setResult(false);
			return json;
		}
		json.setT(book);
		json.check();
		return json;
	}
}
